﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace EGS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly EGSDbContext db;
        public UserController(EGSDbContext context)
        {
            db = context;
        }
        public List<EGS_User> GetAll()
        {
            return db.User.ToList();
        }
        public EGS_User? GetEGS_UserById(Guid id)
        {
            EGS_User? user = new EGS_User();
            user = db.User.Where(u => u.ID == id).FirstOrDefault();
            return user;
        }
        [HttpPost]
        public Result AddUser([FromBody] EGS_User user)
        {
            Result result = new Result();
            db.User.Add(user);
            result.Data = db.SaveChanges();
            return result;
        }
        [HttpPut]
        public Result UpUser([FromBody] EGS_User user)
        {
            Result result = new Result();
            db.User.Update(user);
            result.Data = db.SaveChanges();

            return result;
        }
        [HttpGet]
        public Result Get(Guid id)
        {
            Result result = new Result();
            result.Data = db.User.Where(w => w.ID == id).FirstOrDefault();

            return result;
        }
    }
}
